package com.skit.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import androidx.core.widget.ImageViewCompat
import kotlinx.android.synthetic.main.icon_button.view.*

class IconButton : FrameLayout {
    private var view: View

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        view = LayoutInflater.from(context).inflate(R.layout.icon_button, this, false)
        addView(view)
        attrsSet(attrs)
    }

    private fun attrsSet(attrs: AttributeSet?) {
        val obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.IconButton)

        val string = obtainStyledAttributes.getString(R.styleable.IconButton_text)
        string?.let { text.text = it }

        obtainStyledAttributes.getString(R.styleable.IconButton_subtitle)?.let {
            text2.apply {
                text = it
                visibility = View.VISIBLE
            }
        }

        left_icon.apply {
            val resourceId = obtainStyledAttributes.getResourceId(
                R.styleable.IconButton_icon,
                0
            )
            visibility = if (resourceId != 0) View.VISIBLE else View.GONE
            if (visibility == View.VISIBLE && resourceId != 0) {
                setImageResource(resourceId)
                if (obtainStyledAttributes.hasValue(R.styleable.IconButton_iconTint)) {
                    ImageViewCompat.setImageTintList(
                        this,
                        obtainStyledAttributes.getColorStateList(R.styleable.IconButton_iconTint)
                    )
                }
            }
        }

        if (obtainStyledAttributes.hasValue(R.styleable.IconButton_roundType)) {
            view.apply {
                obtainStyledAttributes.getInt(R.styleable.IconButton_roundType, -1).also {
                    when (it) {
                        0 -> {//all
                            background =
                                ContextCompat.getDrawable(context, R.drawable.selector_round_all)
                        }
                        1 -> {//top
                            background =
                                ContextCompat.getDrawable(context, R.drawable.selector_round_top)
                        }
                        2 -> {//bottom
                            background =
                                ContextCompat.getDrawable(context, R.drawable.selector_round_bottom)
                        }
                        3 -> {//none
                            background =
                                ContextCompat.getDrawable(context, R.drawable.selector_round_none)
                        }
                    }
                }
            }
        }

        right_icon.apply {
            if (obtainStyledAttributes.getBoolean(R.styleable.IconButton_rightVisibility, true)) {
                setImageResource(
                    obtainStyledAttributes.getResourceId(
                        R.styleable.IconButton_rightIcon,
                        R.drawable.ic_round_keyboard_arrow_right_24
                    )
                )
                visibility = View.VISIBLE
                if (obtainStyledAttributes.hasValue(R.styleable.IconButton_rightIconTint)) {
                    ImageViewCompat.setImageTintList(
                        this,
                        obtainStyledAttributes.getColorStateList(R.styleable.IconButton_rightIconTint)
                    )
                }
            } else {
                visibility = GONE
            }

        }

        obtainStyledAttributes.recycle()
    }

    override fun setOnClickListener(l: OnClickListener?) {
//        super.setOnClickListener(l)
        view.setOnClickListener(l)
    }

    fun setText(s: String) {
        text.text = s
    }

    fun setSubText(s: String) {
        text2.visibility = View.VISIBLE
        text2.text = s
    }

}